from flask import Blueprint, request
from flask_restful import Api, Resource, marshal, reqparse

from common.models.stu import User, db, Channel, News, Comment
from .fileds import *

workbp = Blueprint("workbp", __name__)
api = Api(workbp)


class AddUser(Resource):
    def post(self):
        pars = reqparse.RequestParser()
        pars.add_argument("name")
        pars.add_argument("pwd")
        pars.add_argument("mobile")
        pars.add_argument("is_media")
        args = pars.parse_args()
        user_name = args["name"]
        password = args["pwd"]
        mobile = args["mobile"]
        is_media = 0
        if args["is_media"]:
            is_media = int(args["is_media"])
        users = User.query.filter_by(mobile=mobile).count()
        if users:
            return {"code": 400, "msg": "该手机号已被注册"}
        user = User(user_name=user_name, password=password, mobile=mobile, is_media=is_media)
        db.session.add(user)
        db.session.commit()

        return {"code": 200, "msg": "注册成功"}


class AddChannel(Resource):
    """
    添加频道
    """

    def post(self):
        pars = reqparse.RequestParser()
        pars.add_argument("name")
        pars.add_argument("sequence")
        pars.add_argument("is_visible")
        pars.add_argument("is_default")
        args = pars.parse_args()
        is_default = 0
        is_visible = 0
        if args["is_default"]:
            is_default = int(args["is_default"])
        if args["is_visible"]:
            is_visible = int(args["is_visible"])
        chan = Channel.query.filter_by(cname=args["name"]).count()
        if chan != 0:
            return {"code": 400, "msg": "该频道已存在"}
        try:
            channel = Channel(cname=args["name"], sequence=args["sequence"],
                              is_visible=is_visible, is_default=is_default)
            db.session.add(channel)
            db.session.commit()
        except Exception as e:
            print(e)
            return {"code": 500, "msg": "添加失败"}
        return marshal(channel, channel_filesd)


class AddNew(Resource):
    def post(self):
        pars = reqparse.RequestParser()
        pars.add_argument("user_id")
        pars.add_argument("channel_id")
        pars.add_argument("title")
        args = pars.parse_args()
        try:
            new = News(user_id=int(args["user_id"]), channel_id=int(args["channel_id"]),
                       title=args["title"])
            db.session.add(new)
            db.session.commit()
        except Exception:
            return {"code": 500, "msg": "添加失败"}
        return marshal(new, new_fileds)


class AddComment(Resource):
    def get(self):
        article_id = request.form.get("article_id")
        print(">>>>", article_id)
        comment = Comment.query.filter_by(article_id=int(article_id)).all()
        return marshal(comment, comment_fileds)

    def post(self):
        pars = reqparse.RequestParser()
        pars.add_argument("user_id")
        pars.add_argument("article_id")
        pars.add_argument("content")
        args = pars.parse_args()
        try:
            comment = Comment(user_id=int(args["user_id"]), article_id=int(args["article_id"]),
                              content=args["content"])
            db.session.add(comment)
            db.session.commit()
        except Exception:
            return {"code": 500, "msg": "添加失败"}
        return marshal(comment, comment_fileds)


api.add_resource(AddUser, "/add_user")
api.add_resource(AddChannel, "/add_chan")
api.add_resource(AddNew, "/add_new")
api.add_resource(AddComment, "/add_comment")
